package com.atguign.gulimall.product.service.impl;

import com.atguign.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguign.gulimall.product.dao.AttrDao;
import com.atguign.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguign.gulimall.product.entity.AttrEntity;
import com.atguign.gulimall.product.service.AttrService;
import com.atguign.gulimall.product.vo.AttrGroupRelationVo;
import com.atguign.gulimall.product.vo.AttrGroupWithAttrVo;
import com.atguign.gulimall.product.vo.SpuItemAttrGroupVo;
import com.atguigu.common.constant.ProductConstant;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguign.gulimall.product.dao.AttrGroupDao;
import com.atguign.gulimall.product.entity.AttrGroupEntity;
import com.atguign.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    AttrDao attrDao;

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catlogId) {
        String key = (String) params.get("key");
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
        if (!(key == null || key.isEmpty())) {
            wrapper.and((obj) -> {
                obj.eq(AttrGroupEntity::getAttrGroupId, key);
                obj.or().like(AttrGroupEntity::getAttrGroupName, key);
            });
        }
        IPage<AttrGroupEntity> page;
        if (catlogId == 0) {
            page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
        } else {
            wrapper.eq(AttrGroupEntity::getCatelogId, catlogId);
            page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        }
        return new PageUtils(page);
    }

    /**
     * 根据分组id查找关联属性
     *
     * @param attrgroupId
     * @return
     */
    @Override
    public List<AttrEntity> getRelationAttr(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationDao.selectList(
                new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                        .eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId)
        );

        if (attrAttrgroupRelationEntities == null || attrAttrgroupRelationEntities.isEmpty()) {
            return null;
        }
        List<Long> collect = attrAttrgroupRelationEntities.stream()
                .map(AttrAttrgroupRelationEntity::getAttrId)
                .collect(Collectors.toList());
        return attrDao.selectList(new LambdaQueryWrapper<AttrEntity>()
                .in(AttrEntity::getAttrId, collect));
    }

    /**
     * 删除关联关系
     *
     * @param list
     */
    @Override
    public void deleteReation(List<AttrGroupRelationVo> list) {
        LambdaQueryWrapper<AttrAttrgroupRelationEntity> wrapper = new LambdaQueryWrapper<>();
        list.forEach((attrGroupRelationVo -> {
            wrapper.or().eq(AttrAttrgroupRelationEntity::getAttrId, attrGroupRelationVo.getAttrId());
            wrapper.eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupRelationVo.getAttrGroupId());
        }));
        attrAttrgroupRelationDao.delete(wrapper);
    }

    @Override
    public PageUtils getNoRelationAttr(Map<String, Object> params, Long attrgroupId) {
        AttrGroupEntity attrGroupEntity = baseMapper.selectById(attrgroupId);
        List<AttrGroupEntity> attrGroupEntities = baseMapper.selectList(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, attrGroupEntity.getCatelogId()));
        List<Long> collect = attrGroupEntities.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());

        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationDao.selectList(
                new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                        .in(AttrAttrgroupRelationEntity::getAttrGroupId, collect)
        );
        LambdaQueryWrapper<AttrEntity> wrapper = new LambdaQueryWrapper<>();
        if (attrAttrgroupRelationEntities != null && !attrAttrgroupRelationEntities.isEmpty()) {
            List<Long> collect1 = attrAttrgroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
            wrapper.notIn(AttrEntity::getAttrId, collect1);
        }
        String key = (String) params.get("key");
        wrapper.eq(AttrEntity::getAttrType, ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());
        wrapper.eq(AttrEntity::getCatelogId, attrGroupEntity.getCatelogId());
        if (StringUtils.hasText(key)) {
            wrapper.and((obj) -> {
                obj.eq(AttrEntity::getAttrId, key);
                obj.or().like(AttrEntity::getAttrName, key);
            });
        }

        IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params), wrapper);

        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupWithAttrVo> getAttrGroupWithAttrs(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = this.list(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, catelogId));
        if (attrGroupEntities == null || attrGroupEntities.isEmpty()) return null;
        return attrGroupEntities.stream().map(attrGroupEntity -> {
            AttrGroupWithAttrVo attrVo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(attrGroupEntity, attrVo);
            List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationDao.selectList(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                    .eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupEntity.getAttrGroupId()));
            if (attrAttrgroupRelationEntities == null || attrAttrgroupRelationEntities.isEmpty()) return attrVo;
            List<Long> collect = attrAttrgroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
            List<AttrEntity> entities = attrDao.selectBatchIds(collect);
            attrVo.setAttrs(entities);
            return attrVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long skuId, Long catalogId) {
        return baseMapper.getSpuItemAttrGroups(skuId,catalogId);
    }
}